<template>
    <div id="problem-record" class="w-h-100 pos-r flex-1 apply-container page-public-s" style="box-sizing: border-box;overflow:hidden;">
        <div class="w-h-100 ver-flex pos-a" style="box-sizing: border-box; padding: 6px;">
            <div v-if="$hasPer('IssueRecord:excel')" class="search-s page-public-s" style="box-sizing: border-box;">
                <el-card shadow="never" :body-style="{ paddingBottom: '0' }">
                    <el-form class="box-f" ref="queryForm" :inline="true">
                        <el-form-item>
                            <el-button @click="handleExport" icon="ele-Download"> 导出数据 </el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </div>

            <div style="box-sizing: border-box;" class="flex-1 pos-r">
                <div class="pos-a w-h-100" style="box-sizing: border-box;">
                    <el-card class="box-card w-h-100" shadow="never" style="margin-top: 16px;">
                        <div style="" class="w-h-100 ver-flex">
                            <div class="flex-1 pos-r">
                                <div class="pos-a w-h-100">
                                    <el-table ref="excelRef" :data="list" class="w-h-100" style="width: 100%" border v-loading="loading" element-loading-background="rgba(0, 0, 0, 0.5)">

                                        <!-- <el-table-column type="index" label="序号" width="55" :show-overflow-tooltip="true" align="center" :index="indexMethod" /> -->

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="任务名称"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="s_Task" typeVal="select" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_IPTaskName">{{ scope.row.v_IPTaskName }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="风险分析单元"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="s_Analysis" typeVal="select" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_AnalysisUnitName">{{ scope.row.v_AnalysisUnitName }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="200" :show-overflow-tooltip="true" label="问题描述"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" min-width="200" align="center">
                                                <template #header>
                                                    <SearchModel typeId="i_Reason" typeVal="input" @searchHandel="searchHandel" :basicsInfo="basicsInfo" />
                                                </template>
                                                <template #default="scope">
                                                    <span v-if="scope.row.v_Reason">{{ scope.row.v_Reason }}</span>
                                                    <span v-else> - </span>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                        <el-table-column min-width="180" :show-overflow-tooltip="true" prop="createdTime" label="创建时间"  align="center" sortable>
                                            <template #default="scope">
                                                <span v-if="scope.row.createdTime">{{ scope.row.createdTime }}</span>
                                                <span v-else>-</span>
                                            </template>
                                        </el-table-column>

                                        <el-table-column min-width="180" :show-overflow-tooltip="true" prop="updatedTime" label="更新时间"  align="center" sortable>
                                            <template #default="scope">
                                                <span v-if="scope.row.updatedTime">{{ scope.row.updatedTime }}</span>
                                                <span v-else>-</span>
                                            </template>
                                        </el-table-column>

                                        <el-table-column width="200" :show-overflow-tooltip="true" prop="v_Reason" label="操作"  align="center">
                                            <el-table-column :show-overflow-tooltip="true" width="200" align="center">
                                                <template #header>
                                                    <el-button class="icon-e" @click='searchClick' icon="ele-Search" size="small" type="primary" text> 查询 </el-button>
                                                    <el-button class="icon-e clear-bg" style="background-color: transparent !important;" size="small" @click="resetClick" icon="ele-Refresh" text> 重置 </el-button>
                                                </template>
                                                <template #default="scope">
                                                    <el-button v-if="$hasPer('IssueRecord:img')" @click="funClick(scope.row)" class="icon-e" icon="ele-View" size="small" text type="warning">现场图片</el-button>
                                                    <el-button v-else class="icon-e" size="small" text type="danger">暂无操作权限</el-button>
                                                </template>
                                            </el-table-column>
                                        </el-table-column>

                                    </el-table>
                                </div>
                            </div>
                            <div style="height: 30px;margin-top: 20px;" class="hr-flex-start">
								<el-pagination
									v-model:currentPage="pageNo"
									v-model:page-size="pageSize"
									:total="totalRows"
									:page-sizes="[10, 20, 50, 100]"
									small
									background
									@size-change="handleSizeChange"
									@current-change="handleCurrentChange"
									layout="total, sizes, prev, pager, next, jumper"
									class="w-h-100"
								/>
							</div>
                        </div>
                    </el-card>
                </div>
            </div>
        </div>
        <ShowFile
            preType="job"
            :repairInfo="repairInfo"
            :isUseDialog="isUseDialog"
            @cancelUseClick="cancelUseClick"
        />
    </div>
</template>
<script lang="ts">
import { defineComponent, ref, Ref, onMounted , reactive , toRaw } from 'vue';
import getTableData from '/@/utils/getTableData';
import { SMSIPReasonRecordPage , SMSIPTaskList , SMSDCAnalysisUnitList } from '/@/api/centerControl/sms';
import ShowFile from '../component/sms/show-file.vue';
import SearchModel from '../component/search-model.vue';
import { ElMessage } from 'element-plus';
import { exportToExcel } from '/@/utils/exportToExcel';

export default defineComponent( {
    name: 'problem-record',
    components: { ShowFile , SearchModel },
    setup(props){
        const repairInfo = ref('');
        const isUseDialog = ref(false);

        const funClick = (val: any) => {
            repairInfo.value = JSON.stringify(val);
            isUseDialog.value = true;
        }

        const cancelUseClick = () => {
            isUseDialog.value = false;
        }
        
        /*** ================================= 查询类数据操作 =================================  */
        let searchObj:any = reactive({
            n_IPClass: 0
        });
        const basicsInfo: any = ref([]);
        const searInfo: any = ref();

        /*** 获取需要查询的数据 */
        const searchHandel = (val: any) => {
            searInfo.value = val;
        }

        /*** 查询按钮 */
        const searchClick = () => {
            let newObj = Object.keys(searInfo.value);
            if(newObj.length !== 0){
                newObj.map((i:any) => {
                    searchObj[i] = searInfo.value[i];
                });
            }
        };

        /*** 重置按钮 */
        const resetClick = () => {
            searInfo.value = {};
            let newObj = Object.keys(searchObj);
            if(newObj.length !== 0){
                newObj.map((i:any) => {
                    searchObj[i] = ''
                });
            }
            searchObj.n_IPClass = 0;
            initInfoFun();
        };

        let taskArr: any = [];
        let analysisArr: any = [];
        // 获取数据
        const getFun = async () => {
            let ras: any = await SMSIPTaskList({}); // 巡检任务
            let rbs: any = await SMSDCAnalysisUnitList({}); // 风险分析单元
            if(ras.success && ras.data.length !== 0){
                ras.data.map((item: any) => {
                    item.label = item.v_Name;
                    item.value = item.id;
                });
                taskArr = ras.data;
            }
            if(rbs.success && rbs.data.length !== 0){
                rbs.data.map((item: any) => {
                    item.label = item.v_RiskPartitionName ? item.v_RiskPartitionName + '——' + item.v_Name : item.v_Name;
                    item.value = item.id;
                });
                analysisArr = rbs.data;
            }
        }
        
        /*** 初始化数据 */
        const initInfoFun = () => {
            basicsInfo.value = [{
                    typeId: 's_Task', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'l_IPTaskId', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '巡检任务', // placeholder数据
                    defaultProp: taskArr, // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: '', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: '', // 事件名称 --- 选填
                },{
                    typeId: 's_Analysis', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'l_AnalysisUnitId', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '风险分析单元', // placeholder数据
                    defaultProp: analysisArr, // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: '', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: '', // 事件名称 --- 选填
                },{
                    typeId: 'i_Reason', // 类型(不能为空，且唯一，与组件中typeId值对应) --- 必填
                    typeNam: '', // 输入类型(number,text等) --- 选填
                    searchVal: 'v_Reason', // 查询字段 --- 必填
                    defaultVal: '', // 字段默认值 --- 选填
                    placeholder: '问题描述', // placeholder数据
                    defaultProp: '', // 下拉框中使用，除下拉框外其它情况为空 --- 选填
                    eventType: '', // 事件类型（如burl/change等） --- 选填
                    defaultEvent: '', // 事件名称 --- 选填
                }
            ]
        }
        
        onMounted(async () => {
            await getFun();
            initInfoFun(); 
        });
        /*** ================================= 查询类数据操作 =================================  */

        /*** ================================= 导出数据函数操作 ================================= */
        const excelRef: any = ref(null);
        const handleExport = () => {
            if(excelRef.value){
                if(excelRef.value.data.length !== 0){
                    let tableExcel: any = [];
                    let tableArr = excelRef.value.data;
                    tableArr.map((item: any) => {
                        let tableObj: any = {};
                        tableObj['任务名称'] = item.v_IPTaskName ? item.v_IPTaskName : '-';
                        tableObj['风险分析单元'] = item.v_AnalysisUnitName ? item.v_AnalysisUnitName : '-';
                        tableObj['问题描述'] = item.v_Reason ? item.v_Reason : '-';
                        tableExcel.push(tableObj);
                    });
                    exportToExcel(tableExcel,'设备类问题记录台账.xlsx');
                }else{
                    ElMessage({
                        type: 'error',
                        message: '暂无数据导出',
                    })
                }
            }
        }
        /*** ================================= 导出数据函数操作 ================================= */

        return {
            ...getTableData(SMSIPReasonRecordPage,{},searchObj),
            funClick,
            repairInfo,
            isUseDialog,
            cancelUseClick,

            // 导出Excel
            excelRef,
            handleExport,

            // 查询事件
            basicsInfo,
            searchHandel,
            searchClick,
            resetClick,
        }
    },
} )
</script>
<style lang="scss"> @import '../style/public.scss'; </style>
<style scoped lang="scss">
    ul {
        list-style: none;
        li {
            cursor: pointer;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            text-align: center;
            padding: 10px 0;
        }
    }
    #problem-record {
        .box-f{
            :deep(.el-form-item__label){
                padding: 0;
            }
        }
        :deep(.el-card__body) {
            height: 100%;
        }
        :deep(.el-card){
            border: none !important;
        }
        .upload-demo{
            margin-left: 12px;
            margin-top: 2px;
        }
        .icon-e{
            :deep(.el-icon){
                margin-right: 0px!important;
            }
        }
    }
.clear-bg{
    background-color: transparent !important;
}
</style>